CREATE PROCEDURE [dbo].[asi_ReApplyPayments]
@creditInvDistCreated bit = 0 output,
@debugging bit = 0
AS
DECLARE @pmtKey uniqueidentifier
DECLARE @srcInvoiceKey uniqueidentifier
DECLARE @srcInvLineKey uniqueidentifier
DECLARE @pmtAppKey uniqueidentifier
DECLARE @orderNumber nvarchar(50)
DECLARE @orderLineNumber int
DECLARE @paymentScheduleLineKey uniqueidentifier
DECLARE @paymentNumber int
DECLARE @dueDate datetime
DECLARE @amountDue decimal(18,4)
DECLARE @outstandingBalance decimal(18,4)
DECLARE @glAccountKey uniqueidentifier
DECLARE @invoiceKey uniqueidentifier
DECLARE @invoiceLineKey uniqueidentifier
DECLARE @invoiceKeyTarget uniqueidentifier
DECLARE @invoiceLineKeyTarget uniqueidentifier
DECLARE @pmtAmt decimal(18,4)
DECLARE @quantitySold decimal(18,4)
DECLARE @finUnitKey uniqueidentifier
DECLARE @incomeAcctKey uniqueidentifier
DECLARE @ARAcctKey uniqueidentifier
DECLARE @amtAppliedToParentLine decimal(18,4)
DECLARE @amtToApplyToDistLine decimal(18,4)
DECLARE @invDistKey uniqueidentifier
DECLARE @priority int
DECLARE @lineAmountRemaining decimal(18,4)
DECLARE @applicationAmtRemaining decimal(18,4)
DECLARE @extIncome decimal(18,4)
DECLARE @extIncomeRecognized decimal(18,4)
DECLARE @acctMethod nvarchar(50)
DECLARE @appliedTotal decimal(18,4)
DECLARE @applyingCredit bit
DECLARE @componentIncome decimal(18,4)
DECLARE @unitIncome decimal(18,4)
DECLARE @actualAmount decimal(18,4)
DECLARE @discountTaken decimal(18,4)
DECLARE @monetaryAppKey uniqueidentifier
DECLARE @appAmount decimal(18,4)
DECLARE @discountRemaining decimal(18,4)
DECLARE @proportionedDiscount decimal(18,4)
DECLARE @zeroApplication bit
DECLARE @amountToApplyToScheduleLine decimal(18,4)
DECLARE @amountToApplyToDistribution decimal(18,4)
DECLARE @minOfScheduleLineDistributionAmounts decimal(18,4)
DECLARE @amountToApply decimal(18,4)
DECLARE @applyToPaymentScheduleFirst bit
DECLARE @transactionType int
DECLARE @transactionDate datetime
DECLARE @batchKey uniqueidentifier
DECLARE @invoiceNumber nvarchar(50)
SET @zeroApplication = 0
SET @applicationAmtRemaining = 0
set @lineAmountRemaining = 0
SET @amtAppliedToParentLine = 0
SET @pmtAmt = 0
DECLARE Get_Pmts CURSOR FOR
SELECT SrcPaymentKey, SrcInvoiceLineKey, ISNULL(Amount,0), MonetaryApplicationKey, ISNULL(AppliedAmount,0), OrderNumber,
OrderLineNumber,InvoiceKey,InvoiceLineKey,InvoiceDistributionKey, SrcInvoiceKey, DiscountTaken,
TransactionDate, TransactionType, ApplicationBatchKey
FROM #tmpPayments
WHERE OrderNumber IS NOT NULL OR OrderLineNumber IS NOT NULL OR InvoiceKey IS NOT NULL OR InvoiceLineKey IS NOT NULL
ORDER BY OrderNumber asc, OrderLineNumber desc, PaymentTypePriority asc, InvoiceLineKey desc
OPEN Get_Pmts
FETCH NEXT FROM Get_Pmts INTO @pmtKey,@srcInvLineKey, @pmtAmt,@pmtAppKey, @amtAppliedToParentLine,
@orderNumber, @orderLineNumber, @invoiceKey, @invoiceLineKey, @invDistKey, @srcInvoiceKey, @discountTaken,
@transactionDate, @transactionType, @batchKey
WHILE @@FETCH_STATUS = 0
BEGIN
SET @zeroApplication = 0
SET @actualAmount = @amtAppliedToParentLine - @discountTaken
SET @applicationAmtRemaining = @actualAmount
SET @discountRemaining = @discountTaken
IF @invDistKey is not null
GOTO nextPmtApp
IF @invoiceKey is not null AND @invoiceLineKey is null
BEGIN
SET @applyToPaymentScheduleFirst = 1
DECLARE ApplyToScheduleLines CURSOR FOR
SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
FROM PaymentScheduleLine psl
INNER JOIN InvoiceMain im on psl.InvoiceKey = im.InvoiceKey
WHERE im.InvoiceKey = @invoiceKey AND OutstandingBalance > 0
ORDER BY PaymentNumber ASC, PayPriority ASC
DECLARE ApplyToDistributionLines cursor for
SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
FROM #tmpInvoice
WHERE #tmpInvoice.InvoiceKey = @invoiceKey
ORDER BY PayPriority ASC
END
ELSE IF @orderNumber is not null AND LEN(@orderNumber) > 0 AND (@orderLineNumber is null OR @orderLineNumber <= 0)
BEGIN
SET @applyToPaymentScheduleFirst = 1
DECLARE ApplyToScheduleLines CURSOR FOR
SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
FROM PaymentScheduleLine psl
INNER JOIN InvoiceMain im on psl.InvoiceKey = im.InvoiceKey
WHERE im.OrderNumber = @orderNumber AND OutstandingBalance > 0
ORDER BY PaymentNumber ASC, PayPriority ASC
DECLARE ApplyToDistributionLines cursor for
SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
FROM #tmpInvoice
WHERE OrderNumber = @orderNumber
ORDER BY PayPriority ASC
END
ELSE IF @invoiceLineKey is not null
BEGIN
SET @applyToPaymentScheduleFirst = 0
SELECT @componentIncome = SUM(ISNULL(ExtendedIncome,0)) from #tmpInvoice
WHERE ParentInvoiceLineKey = @invoiceLineKey
IF @componentIncome > 0
BEGIN
DECLARE GetApplyToLines cursor for
SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
FROM #tmpInvoice
WHERE ParentInvoiceLineKey = @invoiceLineKey
ORDER BY PayPriority ASC
END
ELSE
BEGIN
DECLARE GetApplyToLines cursor for
SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, OrderLineNumber, InvoiceNumber
FROM #tmpInvoice
WHERE InvoiceLineKey = @invoiceLineKey
ORDER BY PayPriority ASC
END
DECLARE ApplyToScheduleLines CURSOR FOR
SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
FROM InvoiceLine il
INNER JOIN PaymentScheduleLine psl on psl.InvoiceKey = il.InvoiceKey
WHERE il.InvoiceLineKey = @invoiceLineKey AND OutstandingBalance > 0
ORDER BY psl.PaymentNumber ASC, psl.PayPriority ASC
END
ELSE IF @orderLineNumber is not null AND @orderLineNumber > 0
BEGIN
SET @applyToPaymentScheduleFirst = 0
SELECT @componentIncome = SUM(ISNULL(ExtendedIncome,0)) from #tmpInvoice
WHERE ParentInvoiceLineKey in
(SELECT InvoiceLineKey
FROM #tmpInvoice WHERE OrderLineNumber = @orderLineNumber AND OrderNumber = @orderNumber)
IF @componentIncome > 0
BEGIN
DECLARE GetApplyToLines cursor for
SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, @orderLineNumber, InvoiceNumber
FROM #tmpInvoice
WHERE ParentInvoiceLineKey in
(SELECT InvoiceLineKey FROM #tmpInvoice WHERE OrderLineNumber = @orderLineNumber AND OrderNumber = @orderNumber)
ORDER BY PayPriority ASC
END
ELSE
BEGIN
DECLARE GetApplyToLines cursor for
SELECT InvoiceKey, InvoiceLineKey, InvoiceDistributionKey, PayPriority, ISNULL(ExtendedIncome,0), ISNULL(ExtendedIncomeRecognized,0), AccountingMethodCode, AppliedTotal, ApplyingCredit, QuantitySold, FinancialEntityKey, IncomeGLAccountKey, ARGLAccountKey, UnitIncome, @orderLineNumber, InvoiceNumber
FROM #tmpInvoice
WHERE OrderLineNumber = @orderLineNumber AND OrderNumber = @orderNumber
ORDER BY PayPriority ASC
END
DECLARE ApplyToScheduleLines CURSOR FOR
SELECT PaymentScheduleLineKey, PaymentNumber, DueDate, AmountDue, OutstandingBalance, GLAccountKey
FROM InvoiceLine il
INNER JOIN PaymentScheduleLine psl on psl.InvoiceKey = il.InvoiceKey
WHERE il.InvoiceLineKey in
(SELECT InvoiceLineKey from #tmpInvoice
WHERE OrderLineNumber = @orderLineNumber)
AND OutstandingBalance > 0
ORDER BY psl.PaymentNumber ASC, psl.PayPriority ASC
END
ELSE
GOTO nextPmtApp
IF @applyToPaymentScheduleFirst = 1
BEGIN
OPEN ApplyToScheduleLines
FETCH next from ApplyToScheduleLines into
@paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
WHILE @@FETCH_STATUS = 0 AND (ABS(@applicationAmtRemaining) > 0 OR ABS(@discountRemaining) > 0 OR @zeroApplication = 1)
BEGIN
SET @amountToApplyToScheduleLine = @outstandingBalance
OPEN ApplyToDistributionLines
FETCH next from ApplyToDistributionLines into
@invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @extIncome, @extIncomeRecognized, @acctMethod,
@appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey,
@unitIncome, @orderLineNumber, @invoiceNumber
WHILE @@FETCH_STATUS = 0 AND
@amountToApplyToScheduleLine > 0 AND
(ABS(@applicationAmtRemaining) > 0 OR ABS(@discountRemaining) > 0 OR @zeroApplication = 1)
BEGIN
IF @invDistKey is null OR @ARAcctKey != @glAccountKey
GOTO FetchNextDistribution
IF @applicationAmtRemaining > 0 OR @discountRemaining > 0
SET @amountToApplyToDistribution = @extIncome - @appliedTotal
ELSE
SET @amountToApplyToDistribution = @appliedTotal
IF @amountToApplyToDistribution = 0
GOTO FetchNextDistribution
IF ABS(@amountToApplyToScheduleLine) <= ABS(@amountToApplyToDistribution)
SET @minOfScheduleLineDistributionAmounts = @amountToApplyToScheduleLine
ELSE
SET @minOfScheduleLineDistributionAmounts = @amountToApplyToDistribution
SET @amountToApply = @minOfScheduleLineDistributionAmounts
IF ABS(@applicationAmtRemaining) < ABS(@amountToApply)
SET @amountToApply = @applicationAmtRemaining
IF ABS(@discountRemaining) > 0
BEGIN
IF @discountRemaining > 0
SET @proportionedDiscount = @minOfScheduleLineDistributionAmounts - @amountToApply
ELSE
SET @proportionedDiscount = -1 * (@minOfScheduleLineDistributionAmounts + @amountToApply)
IF ABS(@proportionedDiscount) > ABS(@discountRemaining)
SET @proportionedDiscount = @discountRemaining
SET @discountRemaining = @discountRemaining - @proportionedDiscount
END
ELSE
SET @proportionedDiscount = 0
INSERT MonetaryApplication
(MonetaryApplicationKey, SrcPaymentKey, SrcInvoiceLineKey, Amount, InvoiceKey, InvoiceLineKey,
InvoiceDistributionKey, DiscountTaken, OrderNumber, OrderLineNumber, PaymentScheduleLineKey,
TransactionDate, TransactionType, BatchKey, InvoiceNumber)
VALUES (NEWID(), @pmtKey, @srcInvLineKey, @amountToApply, @invoiceKeyTarget, @invoiceLineKeyTarget,
@invDistKey, @proportionedDiscount, @orderNumber, @orderLineNumber, @paymentScheduleLineKey,
@transactionDate, @transactionType, @batchKey, @invoiceNumber)
SET @applicationAmtRemaining = @applicationAmtRemaining - @amountToApply
SET @amountToApplyToScheduleLine = @amountToApplyToScheduleLine - @amountToApply
UPDATE #tmpInvoice SET AppliedTotal = AppliedTotal + @amountToApply
WHERE InvoiceDistributionKey = @invDistKey
UPDATE PaymentScheduleLine SET OutstandingBalance = OutstandingBalance - (@amountToApply + @proportionedDiscount)
WHERE PaymentScheduleLineKey = @paymentScheduleLineKey
FetchNextDistribution:
FETCH next from ApplyToDistributionLines into
@invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @extIncome, @extIncomeRecognized, @acctMethod,
@appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey,
@unitIncome, @orderLineNumber, @invoiceNumber
END
CLOSE ApplyToDistributionLines
FETCH next from ApplyToScheduleLines into
@paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
END
CLOSE ApplyToScheduleLines
DEALLOCATE ApplyToScheduleLines
DEALLOCATE ApplyToDistributionLines
END
ELSE
BEGIN
OPEN GetApplyToLines
FETCH next from GetApplyToLines into
@invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @priority, @extIncome, @extIncomeRecognized, @acctMethod, @appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey, @unitIncome, @orderLineNumber, @invoiceNumber
WHILE @@FETCH_STATUS = 0 AND (ABS(@applicationAmtRemaining) > 0 OR ABS(@discountRemaining) > 0 OR @zeroApplication = 1)
BEGIN
IF @invDistKey is null
GOTO nextApplyToLine
OPEN ApplyToScheduleLines
FETCH next from ApplyToScheduleLines into
@paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
WHILE @@FETCH_STATUS = 0
BEGIN
IF @applicationAmtRemaining > 0 OR @discountRemaining > 0
SET @lineAmountRemaining = @extIncome - @appliedTotal
ELSE
SET @lineAmountRemaining = @appliedTotal
IF @lineAmountRemaining <=0 AND @zeroApplication = 0
GOTO nextApplyToLine
IF @lineAmountRemaining >= ABS(@applicationAmtRemaining)
SET @amtToApplyToDistLine = @applicationAmtRemaining
ELSE
BEGIN
SET @amtToApplyToDistLine = @lineAmountRemaining
IF @applicationAmtRemaining < 0
SET @amtToApplyToDistLine = -1 * @amtToApplyToDistLine
END
IF @outstandingBalance < @amtToApplyToDistLine
SET @amtToApplyToDistLine = @outstandingBalance
IF ABS(@discountRemaining) > 0
BEGIN
IF @discountRemaining > 0
SET @proportionedDiscount = @lineAmountRemaining - @amtToApplyToDistLine
ELSE
SET @proportionedDiscount = -1 * (@lineAmountRemaining + @amtToApplyToDistLine)
IF ABS(@proportionedDiscount) > ABS(@discountRemaining)
SET @proportionedDiscount = @discountRemaining
SET @discountRemaining = @discountRemaining - @proportionedDiscount
END
ELSE
SET @proportionedDiscount = 0
INSERT MonetaryApplication
(MonetaryApplicationKey, SrcPaymentKey, SrcInvoiceLineKey, Amount, InvoiceKey, InvoiceLineKey,
InvoiceDistributionKey, DiscountTaken, OrderNumber, OrderLineNumber, PaymentScheduleLineKey,
TransactionDate, TransactionType, BatchKey, InvoiceNumber)
VALUES (NEWID(), @pmtKey, @srcInvLineKey, @amtToApplyToDistLine, @invoiceKeyTarget, @invoiceLineKeyTarget,
@invDistKey, @proportionedDiscount, @orderNumber, @orderLineNumber, @paymentScheduleLineKey,
@transactionDate, @transactionType, @batchKey, @invoiceNumber)
SET @applicationAmtRemaining = @applicationAmtRemaining - @amtToApplyToDistLine
SET @appliedTotal = @appliedTotal + @amtToApplyToDistLine
UPDATE #tmpInvoice SET AppliedTotal = AppliedTotal + @amtToApplyToDistLine
WHERE InvoiceDistributionKey = @invDistKey
UPDATE PaymentScheduleLine SET OutstandingBalance = OutstandingBalance - (@amtToApplyToDistLine + @proportionedDiscount)
WHERE PaymentScheduleLineKey = @paymentScheduleLineKey
IF @applyingCredit = 1 AND @amtToApplyToDistLine != 0
BEGIN
SET @creditInvDistCreated = 1
INSERT InvoiceDistribution (InvoiceDistributionKey, InvoiceKey, InvoiceLineKey, QuantitySold,
FinancialEntityKey, UnitIncome, ExtendedIncome, ExtendedIncomeRecognized, IncomeGLAccountKey, ARGLAccountKey)
VALUES (NEWID(), @srcInvoiceKey, @srcInvLineKey, @quantitySold, @finUnitKey, -1 * @unitIncome, -1 * @amtToApplyToDistLine, 0, @incomeAcctKey, @ARAcctKey)
END
FETCH next from ApplyToScheduleLines into
@paymentScheduleLineKey, @paymentNumber, @dueDate, @amountDue, @outstandingBalance, @glAccountKey
END
CLOSE ApplyToScheduleLines
nextApplyToLine:
FETCH next from GetApplyToLines into
@invoiceKeyTarget, @invoiceLineKeyTarget, @invDistKey, @priority, @extIncome, @extIncomeRecognized, @acctMethod, @appliedTotal, @applyingCredit, @quantitySold, @finUnitKey, @incomeAcctKey, @ARAcctKey, @unitIncome, @orderLineNumber, @invoiceNumber
END
CLOSE GetApplyToLines
DEALLOCATE GetApplyToLines
DEALLOCATE ApplyToScheduleLines
END
IF ABS(@applicationAmtRemaining) <= 0 AND ABS(@discountRemaining) <= 0
DELETE FROM MonetaryApplication WHERE MonetaryApplicationKey = @pmtAppKey
ELSE IF ABS(@applicationAmtRemaining) < ABS(@actualAmount) OR ABS(@discountRemaining) < ABS(@discountTaken)
UPDATE MonetaryApplication SET Amount = @applicationAmtRemaining, DiscountTaken = @discountRemaining WHERE MonetaryApplicationKey = @pmtAppKey
nextPmtApp:
FETCH NEXT FROM Get_Pmts INTO @pmtKey,@srcInvLineKey, @pmtAmt,@pmtAppKey, @amtAppliedToParentLine,
@orderNumber, @orderLineNumber, @invoiceKey, @invoiceLineKey, @invDistKey, @srcInvoiceKey, @discountTaken,
@transactionDate, @transactionType, @batchKey
END
CLOSE Get_Pmts
DEALLOCATE Get_Pmts
IF @debugging = 1
BEGIN
PRINT 'MonetaryApplication: '
SELECT * from MonetaryApplication
END
GO